<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录</title>
</head>
<body>
  <form action="?" method="POST">
    <div>
      <label for="">账号：</label>
      <input name="account" type="text">
    </div>
    <div>
      <label for="">密码：</label>
      <input name="password" type="text">
    </div>
    <input type="submit" value="登录">
  </form>

  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script>
    layui.use('form', function() {
      var form = layui.form;
  
      // 自定义验证规则
      form.verify({
        userName: function(value, item) {
          if (value.length < 2 || value.length > 6) {
            return '请写正确填写姓名';
          }
        },
        pass : [/(.+){6,12}$/, '密码必须6到12位'],
      });

      // 监听提交
      form.on('submit(updateUser)', function (data) {
        console.log(typeof data);
        console.log(data.field); 
        $.ajax({
          contentType: "application/x-www-form-urlencoded; charset=utf-8",
          type: "post",
          url: htmlVal.htmlUrl + "?doEditUser=",
          data: data.field,
          dataType: 'json',
          success: function(res) {
            isTimeOut(res);
            if (res.errorCode == '0') {
              layer.alert('更改成功', function(index) {
                location.reload();
                layer.close(index);
              })
            }
            if (res.errorCode == 'loginName') {
              layer.alert('登录名已存在！', function(index) {
                layer.close(index);
                return false;
              })
            }
            if (res.errorCode == 'email') {
              layer.alert('email已存在！', function(index) {
                layer.close(index);
                return false;
              })
            }
            if (res.errorCode == 'cellphone') {
              layer.alert('手机号码已存在！', function(index) {
                layer.close(index);
                return false;
              })
            }
            if (res.errorCode == 'wrong_email') {
              layer.alert('email格式错误！', function(index) {
                layer.close(index);
                return false;
              })
            }
            if (res.errorCode == 'wrong_cellphone') {
              layer.alert('手机号码格式错误！', function(index) {
                layer.close(index);
                return false;
              })
            }
          }
        });
        //这个return非常重要，防止ajax跳转，去掉的话就不经过ajax了
        return false; // 阻止默认 form 跳转
      });
    });
  </script>
</body>
</html>